package com.guyu.plugin.satoken.config;

import cn.dev33.satoken.dao.SaTokenDao;
import cn.dev33.satoken.log.SaLog;
import cn.dev33.satoken.strategy.SaAnnotationStrategy;
import com.guyu.plugin.satoken.core.PlusSaTokenDao;
import com.guyu.plugin.satoken.core.SaLogForSlf4j;
import com.guyu.plugin.satoken.exception.SaTokenExceptionHandler;
import com.guyu.plugin.satoken.listener.GuyuSaTokenListener;
import jakarta.annotation.PostConstruct;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.core.annotation.AnnotatedElementUtils;


@AutoConfiguration
public class SaTokenAutoConfiguration {

    /**
     * 自定义dao层存储
     */
    @Bean
    public SaTokenDao saTokenDao() {
        return new PlusSaTokenDao();
    }

    /**
     * 将 Sa-Token log 信息转接到 Slf4j
     */
    @Bean
    public SaLog saLog() {
        return new SaLogForSlf4j();
    }

    /**
     * 重写Sa-Token的注解处理器，增加注解合并功能
     */
    @PostConstruct
    public void rewriteSaStrategy() {
        SaAnnotationStrategy.instance.getAnnotation = AnnotatedElementUtils::getMergedAnnotation;
    }

    /**
     * 异常处理器
     */
    @Bean
    public SaTokenExceptionHandler saTokenExceptionHandler() {
        return new SaTokenExceptionHandler();
    }

    @Bean
    public GuyuSaTokenListener guyuSaTokenListener() {
        return new GuyuSaTokenListener();
    }


}
